﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data.Objects.DataClasses;
using System.Text;
namespace ZhaoPin.Admin
{
    public partial class Qualify : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ReBind();
            }
        }

        protected void btnCheckOk_Click(object sender, EventArgs e)
        {
            string[] ids=Request["CheckBoxGroup"].Split(',');
            StringBuilder sb = new StringBuilder();
            sb.Append("update Person set IsChecked=1,IsCheckedOk=1 where Id in (");
            RecruitContainer rc = new RecruitContainer();
            rc.ExecuteStoreCommand(BuildSqlIn(ids,sb),ids);
            rc.Dispose();
            ReBind();
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            ReBind();
        }

        protected void ReBind()
        {
            RecruitContainer rc = new RecruitContainer();
            IQueryable<Person> match;
            match = from p in rc.People.Include("EduStories").Include("TDictNation") where p.IsFilledTable == true select p;
            IQueryable<Person> m2;
            if (cbArranged.Checked)
            {
                m2 = match.Where(p => p.IsArranged == true);
            }
            else if (cbCheckedBad.Checked && cbChecked.Checked)
            {
                m2 = match.Where(p => p.IsChecked == true);
            }
            else if (cbCheckedBad.Checked)
            {
                m2 = match.Where(p => p.IsChecked == true && p.IsCheckedOk == false);
            }
            else if (cbChecked.Checked)
            {
                m2 = match.Where(p => p.IsChecked == true && p.IsCheckedOk == true);
            }
            else
            {
                m2 = match.Where(p => p.IsChecked == false);
            }
            IQueryable<Person> m3;
            if (tbSearch.Text.Any())
            {
                switch (ddSearchType.SelectedValue)
                {
                    case "0":
                        int i = 0;
                        if (!int.TryParse(tbSearch.Text, out i)) i = -1;
                        m3 = m2.Where(p => p.Sequence == i);
                        break;
                    case "1":
                        m3 = m2.Where(p => p.IdentityCardNumber.Contains(tbSearch.Text));
                        break;
                    case "2":
                        m3 = m2.Where(p => p.RealName.Contains(tbSearch.Text));
                        break;
                    case "3":
                        m3 = m2.Where(p => p.TDictPost.FName.Contains(tbSearch.Text));
                        break;
                    default:
                        m3 = m2;
                        break;
                }
            }
            else
            {
                m3 = m2;
            }
            gvList.DataSource = m3;
            gvList.DataBind();
            rc.Dispose();
        }
        protected static string GetFirstEduLevel(EntityCollection<EduStory> es)
        {
            if(es.Any())
                return es.OrderBy(e=>e.StartDate).First().TDictEduLevel.FName;
            return "";
        }

        protected void btnCheck2Uncheck_Click(object sender, EventArgs e)
        {
            string[] ids = Request["CheckBoxGroup"].Split(',');
            StringBuilder sb = new StringBuilder();
            sb.Append("update Person set IsChecked=0,IsCheckedOk=0 where Id in (");
            RecruitContainer rc = new RecruitContainer();
            rc.ExecuteStoreCommand(BuildSqlIn(ids,sb), ids);
            sb.Clear();
            rc.Dispose();
            ReBind();
        }
        protected string BuildSqlIn(string[] ids, StringBuilder sb)
        {
            for (int i = 0; i < ids.Length; i++)
            {
                sb.Append("{");
                sb.Append(i.ToString());
                sb.Append("},");
            }
            sb.Remove(sb.Length - 1, 1);
            sb.Append(")");
            return sb.ToString();
        }

        protected void btnCheckBadReturnRefill_Click(object sender, EventArgs e)
        {
            string[] ids = Request["CheckBoxGroup"].Split(',');
            StringBuilder sb = new StringBuilder();
            sb.Append("update Person set IsChecked=1,IsCheckedOk=0,IsCanRefillTable=1,RefuseReason='" + tbReason.Text + "'" + " where Id in (");
            RecruitContainer rc = new RecruitContainer();
            rc.ExecuteStoreCommand(BuildSqlIn(ids, sb), ids);
            sb.Clear();
            rc.Dispose();
            ReBind();
        }

        protected void btnCheckBadNotRefill_Click(object sender, EventArgs e)
        {
            string[] ids = Request["CheckBoxGroup"].Split(',');
            StringBuilder sb = new StringBuilder();
            sb.Append("update Person set IsChecked=1,IsCheckedOk=0,IsCanRefillTable=0,RefuseReason='" + tbReason.Text + "'" + " where Id in (");
            RecruitContainer rc = new RecruitContainer();
            rc.ExecuteStoreCommand(BuildSqlIn(ids, sb), ids);
            sb.Clear();
            rc.Dispose();
            ReBind();
        }

        protected void gvList_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow && e.Row.RowIndex > -1)
            {
                string id = gvList.DataKeys[e.Row.RowIndex].Value.ToString();
                e.Row.Attributes.Add("ondblclick", "ShowDetails('"+id+"')");
                e.Row.Attributes.Add("OnMouseOut", "this.style.backgroundColor='White';this.style.color='#000000'");
                e.Row.Attributes.Add("OnMouseOver", "this.style.backgroundColor='#0066cc';this.style.color='#ffff99'");
                e.Row.Attributes["style"] = "Cursor:hand";
            }
        }
    }
}